﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_AddDepartment : BaseXtraForm
    {
        #region 属性
        public EStatus FStatus { get; set; }
        public Guid? DeptID { get; set; }
        public Guid? ParentID { get; set; }
        public Guid? CompanyID { get; set; }
        public event EventHandler SaveEvent;
        #endregion

        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_AddDepartment(EStatus status,Guid? companyID = null,Guid? deptID = null ,Guid? parentID = null)
        {
            InitializeComponent();
            this.DeptID = deptID;
            this.FStatus = status;
            this.ParentID = parentID;
            this.CompanyID = companyID;

        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddDepartment_Load(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            if (FStatus == EStatus.Add)
            {
                this.Text = "部门 - 添加";
            }
            if (FStatus == EStatus.Modify)
            {
                var dept = aservice.GetDeptByID(DeptID);
                EditName.Text = dept.Name;
                EditCode.Text = dept.Code;
                this.Text = "部门 - 修改";
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            Department dept = null;
            if (FStatus == EStatus.Add)
            {
                dept = aservice.AddDept(this.EditName.Text,ParentID,this.EditCode.Text,this.CompanyID);
                this.Hide();
                MessageBox.Show("添加成功!");
            }
            if (FStatus == EStatus.Modify)
            {
                dept = aservice.UpdateDept(DeptID, this.EditName.Text, this.EditCode.Text);
                this.Hide();
                MessageBox.Show("编辑成功!");
            }
            if (SaveEvent != null)
            {
                SaveEvent(dept, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            Close();
        }
        
    }
}
